package cineplanet.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import javax.sql.DataSource;

import cineplanet.dao.IComboDAO;
import cineplanet.excepcion.DAOExcepcion;
import cineplanet.modelo.TabCombo;

public class ComboDAO extends HibernateDaoSupport implements IComboDAO {

	protected Log log = LogFactory.getLog(getClass());
	
	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
		
	@SuppressWarnings("unchecked")
	public TabCombo insertar(TabCombo objTabCombo) throws DAOExcepcion {
		try {
			getHibernateTemplate().save(objTabCombo);
			getHibernateTemplate().flush();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		return objTabCombo;
	}

	@SuppressWarnings("unchecked")
	public TabCombo actualizar(TabCombo objTabCombo) throws DAOExcepcion {
		try {
			getHibernateTemplate().update(objTabCombo);
			getHibernateTemplate().flush();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		return objTabCombo;
	}

	@SuppressWarnings("unchecked")
	public TabCombo obtener(TabCombo objTabCombo) throws DAOExcepcion {
		log.info("obtener() : " + objTabCombo.getCOM_IdCombo());
		TabCombo oTabCombo = null;
		try {
			oTabCombo = (TabCombo) getHibernateTemplate().find(
					"from TabCombo c where c.COM_IdCombo = ?",
					objTabCombo.getCOM_IdCombo()).get(0);
		} catch (Exception e) {
			log.error("Error: " + e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		return oTabCombo;
	}

	@SuppressWarnings("unchecked")
	public List<TabCombo> listar(TabCombo objTabCombo) throws DAOExcepcion {
		log.info("listar()");
		List<TabCombo> lista = null;
		try {
			lista = getHibernateTemplate().find("from TabCombo c where c.COM_IdEstado = ?",objTabCombo.getCOM_IdEstado());
		} catch (DataAccessException e) {
			log.error(e.toString());
			throw new DAOExcepcion(e.getMessage());
		}
		return lista;
	}

}
